home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_os.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  5.2 KB  |  188 lines

  1. # As a test suite for the os module, this is woefully inadequate, but this
  2. # does add tests for a few functions which have been determined to be more
  3. # more portable than they had been thought to be.
  4.  
  5. import os
  6. import unittest
  7. import warnings
  8.  
  9. warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, __name__)
  10. warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, __name__)
  11.  
  12. from test_support import TESTFN, run_unittest
  13.  
  14. class TemporaryFileTests(unittest.TestCase):
  15.     def setUp(self):
  16.         self.files = []
  17.         os.mkdir(TESTFN)
  18.  
  19.     def tearDown(self):
  20.         for name in self.files:
  21.             os.unlink(name)
  22.         os.rmdir(TESTFN)
  23.  
  24.     def check_tempfile(self, name):
  25.         # make sure it doesn't already exist:
  26.         self.failIf(os.path.exists(name),
  27.                     "file already exists for temporary file")
  28.         # make sure we can create the file
  29.         open(name, "w")
  30.         self.files.append(name)
  31.  
  32.     def test_tempnam(self):
  33.         if not hasattr(os, "tempnam"):
  34.             return
  35.         warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
  36.                                 "test_os")
  37.         self.check_tempfile(os.tempnam())
  38.  
  39.         name = os.tempnam(TESTFN)
  40.         self.check_tempfile(name)
  41.  
  42.         name = os.tempnam(TESTFN, "pfx")
  43.         self.assert_(os.path.basename(name)[:3] == "pfx")
  44.         self.check_tempfile(name)
  45.  
  46.     def test_tmpfile(self):
  47.         if not hasattr(os, "tmpfile"):
  48.             return
  49.         fp = os.tmpfile()
  50.         fp.write("foobar")
  51.         fp.seek(0,0)
  52.         s = fp.read()
  53.         fp.close()
  54.         self.assert_(s == "foobar")
  55.  
  56.     def test_tmpnam(self):
  57.         if not hasattr(os, "tmpnam"):
  58.             return
  59.         warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning,
  60.                                 "test_os")
  61.         self.check_tempfile(os.tmpnam())
  62.  
  63. # Test attributes on return values from os.*stat* family.
  64. class StatAttributeTests(unittest.TestCase):
  65.     def setUp(self):
  66.         os.mkdir(TESTFN)
  67.         self.fname = os.path.join(TESTFN, "f1")
  68.         f = open(self.fname, 'wb')
  69.         f.write("ABC")
  70.         f.close()
  71.  
  72.     def tearDown(self):
  73.         os.unlink(self.fname)
  74.         os.rmdir(TESTFN)
  75.  
  76.     def test_stat_attributes(self):
  77.         if not hasattr(os, "stat"):
  78.             return
  79.  
  80.         import stat
  81.         result = os.stat(self.fname)
  82.  
  83.         # Make sure direct access works
  84.         self.assertEquals(result[stat.ST_SIZE], 3)
  85.         self.assertEquals(result.st_size, 3)
  86.  
  87.         import sys
  88.  
  89.         # Make sure all the attributes are there
  90.         members = dir(result)
  91.         for name in dir(stat):
  92.             if name[:3] == 'ST_':
  93.                 attr = name.lower()
  94.                 self.assertEquals(getattr(result, attr),
  95.                                   result[getattr(stat, name)])
  96.                 self.assert_(attr in members)
  97.  
  98.         try:
  99.             result[200]
  100.             self.fail("No exception thrown")
  101.         except IndexError:
  102.             pass
  103.  
  104.         # Make sure that assignment fails
  105.         try:
  106.             result.st_mode = 1
  107.             self.fail("No exception thrown")
  108.         except TypeError:
  109.             pass
  110.  
  111.         try:
  112.             result.st_rdev = 1
  113.             self.fail("No exception thrown")
  114.         except (AttributeError, TypeError):
  115.             pass
  116.  
  117.         try:
  118.             result.parrot = 1
  119.             self.fail("No exception thrown")
  120.         except AttributeError:
  121.             pass
  122.  
  123.         # Use the stat_result constructor with a too-short tuple.
  124.         try:
  125.             result2 = os.stat_result((10,))
  126.             self.fail("No exception thrown")
  127.         except TypeError:
  128.             pass
  129.  
  130.         # Use the constructr with a too-long tuple.
  131.         try:
  132.             result2 = os.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
  133.         except TypeError:
  134.             pass
  135.  
  136.  
  137.     def test_statvfs_attributes(self):
  138.         if not hasattr(os, "statvfs"):
  139.             return
  140.  
  141.         import statvfs
  142.         result = os.statvfs(self.fname)
  143.  
  144.         # Make sure direct access works
  145.         self.assertEquals(result.f_bfree, result[statvfs.F_BFREE])
  146.  
  147.         # Make sure all the attributes are there
  148.         members = dir(result)
  149.         for name in dir(statvfs):
  150.             if name[:2] == 'F_':
  151.                 attr = name.lower()
  152.                 self.assertEquals(getattr(result, attr),
  153.                                   result[getattr(statvfs, name)])
  154.                 self.assert_(attr in members)
  155.  
  156.         # Make sure that assignment really fails
  157.         try:
  158.             result.f_bfree = 1
  159.             self.fail("No exception thrown")
  160.         except TypeError:
  161.             pass
  162.  
  163.         try:
  164.             result.parrot = 1
  165.             self.fail("No exception thrown")
  166.         except AttributeError:
  167.             pass
  168.  
  169.         # Use the constructor with a too-short tuple.
  170.         try:
  171.             result2 = os.statvfs_result((10,))
  172.             self.fail("No exception thrown")
  173.         except TypeError:
  174.             pass
  175.  
  176.         # Use the constructr with a too-long tuple.
  177.         try:
  178.             result2 = os.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
  179.         except TypeError:
  180.             pass
  181.  
  182. def test_main():
  183.     run_unittest(TemporaryFileTests)
  184.     run_unittest(StatAttributeTests)
  185.  
  186. if __name__ == "__main__":
  187.     test_main()
  188.